package cn.edu.neusoft.food729.model;

import java.util.List;

import cn.edu.neusoft.food729.Bean.ShopBean;
import cn.edu.neusoft.food729.common.Common;
import cn.edu.neusoft.food729.iface.Shopiface;
import cn.edu.neusoft.food729.listener.ShopListener;
import cn.edu.neusoft.food729.service.ShopService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ShopModel implements Shopiface {

    private Retrofit retrofit;

    public ShopModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Common.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getResultList(final ShopListener listener) {
        ShopService service = retrofit.create(ShopService.class);
        Call<List<ShopBean>> call = service.getAllShops();

        call.enqueue(new Callback<List<ShopBean>>() {
            @Override
            public void onResponse(Call<List<ShopBean>> call, Response<List<ShopBean>> response) {
                if (response.isSuccessful() && response.body() != null)
                    listener.onResponse(response.body());
                else listener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<List<ShopBean>> call, Throwable t) {
                listener.onFail(t.toString());
                System.out.println("--ArticleModel--" + t.toString());
            }
        });
    }
}

